IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'MD3.f_TableExists'))
   DROP FUNCTION MD3.f_TableExists
GO

-- Function determines if the table exists.

CREATE FUNCTION MD3.f_TableExists (
    @SchemaName SYSNAME
   ,@TableName SYSNAME
   )
    RETURNS BIT
    WITH RETURNS NULL ON NULL INPUT
AS

BEGIN

    -- Remove any brackets.
    SELECT @SchemaName=REPLACE(@SchemaName,'[','')  
    SELECT @SchemaName=REPLACE(@SchemaName,']','')  
    SELECT @TableName=REPLACE(@TableName,'[','')  
    SELECT @TableName=REPLACE(@TableName,']','')  

    -- Check to see if the table exists.
    IF EXISTS (SELECT * FROM sys.objects WHERE name = @TableName AND schema_id = schema_id(@SchemaName) AND type = 'U')
	BEGIN
        RETURN 1
    END
	ELSE
	BEGIN
        RETURN 0
	END;

	RETURN 0

END;
GO
